package com.runfast.waimai.service.impl;

import com.runfast.common.Constants;
import com.runfast.common.service.BaseService;
import com.runfast.waimai.dao.mapper.BasePurviewMapper;
import com.runfast.waimai.dao.model.BasePurview;
import com.runfast.waimai.dao.model.BasePurviewExample;
import com.runfast.waimai.dao.model.BaseUser;
import com.runfast.waimai.service.BasePurviewService;
import com.runfast.waimai.service.BaseRolePurviewService;
import com.runfast.waimai.service.BaseUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class BasePurviewServiceImpl extends BaseService<BasePurview, Integer, BasePurviewExample> implements BasePurviewService {
    @Autowired
    private BaseRolePurviewService baseRolePurviewService;
    /***根据老系统做的*/
    public List<BasePurview> findForUser(BaseUser user, boolean isMenu){
        List<BasePurview> purList = null;
        //Set<BasePurview> purviews = user.getPurviews();
        List<Integer> purviewIdList=user.getPurviewIdList();
        user.getRoleIdList().size();
        for(Integer id:user.getRoleIdList()){
            List<Integer> purviewIdList1=baseRolePurviewService.queryPurviewIdList(id);
            if(purviewIdList1.size()>0){
                //for(Integer id1:purviewIdList1){
                    //purviewIdList.add(id1);
               // }
                purviewIdList.addAll(purviewIdList1);
            }
        }
       if(purviewIdList.size()>0){
        /*user.getRoles().size();
        for(BaseRole role:user.getRoles()){
            purviews.addAll(role.getPurviews());
        }
        if(purviews.size()>0){
            List<Integer> idList = new ArrayList<Integer>();
            for(BasePurview p:purviews){
                if(isMenu){//只是查看菜单的权限
                    if(p.getPtype()==0){
                        idList.add(p.getPid());
                    }
                }else{
                    idList.add(p.getPid());
                }
            }*/
            BasePurviewExample example=new BasePurviewExample();
            example.createCriteria().andPidIn(purviewIdList);
            List<BasePurview> list = mapper.selectByExample(example);
            purList = parsePurview(list);
        }else{
            purList = new ArrayList<BasePurview>();
        }
        return purList;
    }

    /**
     *组织菜单结构 ,根据老系统做的
     * */
    public List<BasePurview> parsePurview(List<BasePurview> list){

        List<BasePurview> rootList = new ArrayList<BasePurview>();
        Iterator<BasePurview> it = list.iterator();
        BasePurview purview = new BasePurview();

        while(it.hasNext()){//一级目录
            purview = it.next();
            if(purview.getByid()==0){
                rootList.add(purview);
                it.remove();
            }
        }

        for(BasePurview rootPurview:rootList){//二级目录
            it = list.iterator();
            while(it.hasNext()){
                purview = it.next();
                //if(rootPurview.getPid().equals(200000)&&purview.getByid().equals(200000)){
                    if(rootPurview.getPid().equals(purview.getByid())){
                        rootPurview.getSubList().add(purview);
                        it.remove();
                    }
                //}

            }
        }



       /* if(list.size()>0){
            for(BasePurview basePurview:list){
                if(basePurview.getByid()!=0){
                    BasePurview  purview1=this.selectByPrimaryKey(basePurview.getByid());
                    purview1.getSubList().add(purview);
                    rootList.add(purview1);
                    it.remove();
                    if(list.size()>0){
                        for(BasePurview basePurview1:list){

                            if(purview1.getPid().equals(basePurview1.getByid())){
                                purview1.getSubList().add(basePurview1);
                                //it.remove();
                            }
                        }
                    }
                }
            }
        }*/

        /*for(BasePurview rootPurview:rootList){//三级目录
            List<BasePurview> subList = rootPurview.getSubList();
            for(BasePurview subPurview:subList){
                it = list.iterator();
                while(it.hasNext()){
                    purview = it.next();
                    if(subPurview.getPid()==purview.getByid()){
                        subPurview.getSubList().add(purview);
                    }
                }
            }
        }*/

        return rootList;
    }

    @Autowired
    private BaseUserService baseUserService;
    public List<BasePurview> getUserPurviewList(Integer userId){
        //系统管理员，拥有最高权限
        if(userId == Constants.SUPER_ADMIN){
            return getAllMenuList(null);
        }

        //用户菜单列表
        //List<Integer> menuIdList = baseUserService.queryAllPurviewId(userId);
        List<Integer> menuIdList = null;
        return getAllMenuList(menuIdList);
    }

    /**
     * 获取所有菜单列表
     */
    private List<BasePurview> getAllMenuList(List<Integer> menuIdList){
        //查询根菜单列表
        List<BasePurview> menuList = queryListParentId(0,menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    public List<BasePurview> queryListParentId(Integer parentId, List<Integer> menuIdList) {
        List<BasePurview> menuList = queryListParentId(parentId);
        if(menuIdList == null){
            return menuList;
        }

        List<BasePurview> userMenuList = new ArrayList<>();
        for(BasePurview menu : menuList){
            if(menuIdList.contains(menu.getPid())){
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }
    /**
     * 递归
     */
    private List<BasePurview> getMenuTreeList(List<BasePurview> menuList, List<Integer> menuIdList){
        List<BasePurview> subMenuList = new ArrayList<BasePurview>();

        for(BasePurview entity : menuList){
            //目录
            if(entity.getPtype() == Constants.MenuType.CATALOG.getValue()){
                entity.setList(getMenuTreeList(queryListParentId(entity.getPid(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }

    public List<BasePurview> queryListParentId(Integer parentId) {
        BasePurviewMapper basePurviewMapper=(BasePurviewMapper)this.mapper;
        return basePurviewMapper.queryListParentId(parentId);
    }

    public List<BasePurview> queryNotButtonList(){
        BasePurviewMapper basePurviewMapper=(BasePurviewMapper)this.mapper;
        return basePurviewMapper.queryNotButtonList();
    }


}